package com.smalls.redshoes.ui.widget;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.mvp.bean.StreamInfo;
import com.smalls.newvideotwo.service.FloatService;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.redshoes.R;

public class VodPlayControlView extends RelativeLayout {
    public static final int SET_NETWORK_SPEED_INTERVAL = 2000;
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + "PlayBarLayout":"PlayBarLayout";
    private boolean isBuffering;
    private boolean isLoadingNext;
    private boolean isPause;
    private VodPlayControlBottomView mBottomLayout;
    private TextView mHintTv;
    private String mJniErrorString;
    private long mLastSetTime;
    private VodPlayControlBottomView.OnEventListener mOnBottomLayoutEventListener;
    private ImageView mPauseIv;
    private View mRootView;
    private int mState;
    private StreamInfo mStreamingModel;
    private BroadcastReceiver netSpeedReceiver;
    private OnEventListener onEventListener;

    public interface OnEventListener {
        void onProgressChanged(int i);

        void onSeekedComplete(int i);

        void onStartSeek();
    }

    public interface State {
        public static final int STATE_BACKWARD = 6;
        public static final int STATE_BAD_ERROR = -2;
        public static final int STATE_BUFFING = 1;
        public static final int STATE_ERROR = -1;
        public static final int STATE_FORWARD = 7;
        public static final int STATE_JNI_ERROR = -3;
        public static final int STATE_LOADING = 0;
        public static final int STATE_PAUSE = 2;
        public static final int STATE_PLAYING = 3;
        public static final int STATE_PREPARED = 4;
        public static final int STATE_SEEKING = 5;
    }

    public void setState(int state) {
        this.mState = state;
        switch (this.mState) {
            case State.STATE_JNI_ERROR /*-3*/:
                if (TextUtils.isEmpty(this.mJniErrorString)) {
                    this.mHintTv.setVisibility(View.GONE);
                } else {
                    if (this.mHintTv.getVisibility() != View.VISIBLE) {
                        this.mHintTv.setVisibility(View.VISIBLE);
                    }
                    this.mHintTv.setText(this.mJniErrorString);
                }
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(false);
                return;
            case State.STATE_BAD_ERROR /*-2*/:
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                this.mHintTv.setText(getContext().getResources().getString(R.string.loading_bad_error));
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(false);
                return;
            case State.STATE_ERROR /*-1*/:
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                this.mHintTv.setText(getContext().getResources().getString(R.string.loading_error));
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(false);
                return;
            case State.STATE_LOADING /*0*/:
                CharSequence string;
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                TextView textView = this.mHintTv;
                if (this.isLoadingNext) {
                    string = getContext().getResources().getString(R.string.next_epg);
                } else {
                    string = getContext().getResources().getString(R.string.loading);
                }
                textView.setText(string);
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(false);
                this.mBottomLayout.setVisibility(View.VISIBLE);
                this.mBottomLayout.setHasPlayedTime(0);
                this.mBottomLayout.setTotalTime(0);
                this.mBottomLayout.setProgress(0);
                this.mBottomLayout.setIsPrepared(false);
                return;
            case State.STATE_BUFFING /*1*/:
                this.isBuffering = true;
                if (!this.mBottomLayout.isSeeking()) {
                    if (this.mHintTv.getVisibility() != View.VISIBLE) {
                        this.mHintTv.setVisibility(View.VISIBLE);
                    }
                    this.mHintTv.setText(getContext().getResources().getString(R.string.buffering));
                    this.mBottomLayout.setAutoHide(true);
                    this.mPauseIv.setVisibility(View.GONE);
                    LogUtil.d(TAG, "STATE_BUFFING");
                    return;
                }
                return;
            case 2 /*2*/:
                this.isPause = true;
                this.mPauseIv.setImageResource(R.mipmap.rs_play_pause);
                this.mPauseIv.setVisibility(View.VISIBLE);
                this.mHintTv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(true);
                return;
            case 3 /*3*/:
                this.isBuffering = false;
                this.isPause = false;
                this.mPauseIv.clearAnimation();
                this.mHintTv.setVisibility(View.GONE);
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(true);
                LogUtil.d(TAG, "STATE_PLAYING");
                return;
            case 4 /*4*/:
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                this.mHintTv.setText(getContext().getResources().getString(R.string.load_complete));
                this.mBottomLayout.setAutoHide(false);
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setIsPrepared(true);
                return;
            case 5 /*5*/:
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                this.mPauseIv.setVisibility(View.GONE);
                this.mBottomLayout.setAutoHide(false);
                return;
            case 6 /*6*/:
                this.mPauseIv.setImageResource(R.mipmap.rs_ic_backward);
                if (this.mPauseIv.getVisibility() != View.VISIBLE) {
                    this.mPauseIv.setVisibility(View.VISIBLE);
                }
                if (this.mHintTv.getVisibility() == View.VISIBLE) {
                    this.mHintTv.setVisibility(View.GONE);
                }
                this.mBottomLayout.setAutoHide(false);
                this.mBottomLayout.setVisibility(View.VISIBLE);
                return;
            case 7 /*7*/:
                this.mPauseIv.setImageResource(R.mipmap.rs_ic_forward);
                if (this.mPauseIv.getVisibility() != View.VISIBLE) {
                    this.mPauseIv.setVisibility(View.VISIBLE);
                }
                if (this.mHintTv.getVisibility() == View.VISIBLE) {
                    this.mHintTv.setVisibility(View.GONE);
                }
                this.mBottomLayout.setAutoHide(false);
                this.mBottomLayout.setVisibility(View.VISIBLE);
                return;
            default:
                if (this.mHintTv.getVisibility() != View.VISIBLE) {
                    this.mHintTv.setVisibility(View.VISIBLE);
                }
                this.mHintTv.setText(getContext().getResources().getString(R.string.loading_error));
                this.mPauseIv.setVisibility(View.GONE);
                return;
        }
    }

    public void sranslateRight(View view) {
        Animation mHideListAnim = new TranslateAnimation(1, 0.0f, 1, 4.5f, 1, 0.0f, 1, 1.2f);
        mHideListAnim.setDuration(1000);
        mHideListAnim.setFillAfter(true);
        view.startAnimation(mHideListAnim);
    }

    public void setLoadingNext(boolean isLoadingNext) {
        this.isLoadingNext = isLoadingNext;
    }

    public VodPlayControlView(Context context) {
        this(context, null);
    }

    public VodPlayControlView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VodPlayControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mJniErrorString = "";
        this.mState = 0;
        this.isLoadingNext = false;
        this.isPause = false;
        this.isBuffering = false;
        this.mOnBottomLayoutEventListener = new VodPlayControlBottomView.OnEventListener() {
            public void onSeekLeft() {
                VodPlayControlView.this.setState(6);
                if (VodPlayControlView.this.onEventListener != null) {
                    VodPlayControlView.this.onEventListener.onStartSeek();
                }
            }

            public void onSeekRight() {
                VodPlayControlView.this.setState(7);
                if (VodPlayControlView.this.onEventListener != null) {
                    VodPlayControlView.this.onEventListener.onStartSeek();
                }
            }

            public void onSeekBarSeekedComplete(int progress) {
                if (VodPlayControlView.this.onEventListener != null) {
                    VodPlayControlView.this.onEventListener.onSeekedComplete(progress);
                }
            }

            public void onProgressChanged(int progress) {
                if (VodPlayControlView.this.onEventListener != null) {
                    VodPlayControlView.this.onEventListener.onProgressChanged(progress);
                }
            }
        };
        this.mLastSetTime = 0;
        this.netSpeedReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(FloatService.ActionNetSpeedChanged)) {
                    String text = intent.getStringExtra(FloatService.KeyNetSpeedChanged);
                    if (!TextUtils.isEmpty(text) && VodPlayControlView.this.mBottomLayout != null && SystemClock.uptimeMillis() - VodPlayControlView.this.mLastSetTime >= 2000L) {
                        VodPlayControlView.this.mBottomLayout.setNetSpeed(text);
                        VodPlayControlView.this.mLastSetTime = SystemClock.uptimeMillis();
                    }
                }
            }
        };
        initViews(context);
    }

    private void initViews(Context context) {
        this.mRootView = LayoutInflater.from(context).inflate(R.layout.layout_play_bar, this, true);
        this.mPauseIv = (ImageView) this.mRootView.findViewById(R.id.layout_playbar_pause_iv);
        this.mPauseIv.setFocusable(false);
        this.mPauseIv.clearFocus();
        this.mBottomLayout = (VodPlayControlBottomView) this.mRootView.findViewById(R.id.layout_playbar_bottom);
        this.mHintTv = (TextView) this.mRootView.findViewById(R.id.layout_playbar_hint_tv);
        this.mHintTv.setFocusable(false);
        this.mHintTv.clearFocus();
        this.mBottomLayout.setOnEventListener(this.mOnBottomLayoutEventListener);
        registerNetSpeedReceiver();
    }

    public void registerNetSpeedReceiver() {
        getContext().registerReceiver(this.netSpeedReceiver, new IntentFilter(FloatService.ActionNetSpeedChanged));
    }

    public void unregisterNetSpeedReceiver() {
        try {
            getContext().unregisterReceiver(this.netSpeedReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateBottomLayoutVisibility() {
        if (this.mBottomLayout != null) {
            this.mBottomLayout.setVisibility(this.mBottomLayout.getVisibility() == View.VISIBLE ? GONE : VISIBLE);
        }
    }

    public int getState() {
        return this.mState;
    }

    public VodPlayControlBottomView getBottomLayout() {
        return this.mBottomLayout;
    }

    public void setJniErrorString(String mJniErrorString) {
        this.mJniErrorString = mJniErrorString;
        if (this.mState == -3) {
            setState(-3);
        }
    }

    public void setOnEventListener(OnEventListener onEventListener) {
        this.onEventListener = onEventListener;
    }

    public void setHasPlayedTime(long time) {
        if (this.mBottomLayout != null) {
            this.mBottomLayout.setHasPlayedTime(time);
        }
    }

    public void setTotalTime(int time) {
        if (this.mBottomLayout != null) {
            this.mBottomLayout.setTotalTime(time);
        }
    }

    public boolean isSeeking() {
        return this.mBottomLayout == null ? false : this.mBottomLayout.isSeeking();
    }

    public void setProgress(int progress) {
        if (this.mBottomLayout != null) {
            this.mBottomLayout.setProgress(progress);
        }
    }

    public void destory() {
        LogUtil.d(TAG, "destory");
        if (this.mBottomLayout != null) {
            this.mBottomLayout.destory();
        }
        clearResource();
    }

    private void clearResource() {
        unregisterNetSpeedReceiver();
    }

    public SeekBar getSeekBar() {
        return this.mBottomLayout == null ? null : this.mBottomLayout.getSeekBar();
    }

    public void setDuration(int duration) {
        if (this.mBottomLayout != null) {
            this.mBottomLayout.setDuration(duration);
        }
    }

    public View getMenuHintView() {
        return this.mBottomLayout.getMenuHintView();
    }

    public void setStreamingModel(MovieDetail model, int streamingIndex) {
        if (model != null) {
            this.mStreamingModel = (StreamInfo) model.getStreaming().get(streamingIndex);
            if (this.mStreamingModel != null) {
                String streamingName;
                if (model.getStreaming().size() > 1) {
                    streamingName = model.getTitle() + "  " + this.mStreamingModel.getSequence();
                } else {
                    streamingName = model.getTitle();
                }
                if (this.mBottomLayout != null && !TextUtils.isEmpty(streamingName)) {
                    this.mBottomLayout.setStreamingName(streamingName);
                }
            }
        }
    }
}
